package com.hns.can.energy.web;

import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.common.domain.GridPage;
import com.hns.can.common.service.CommonInitParamService;
import com.hns.can.common.web.BaseController;
import com.hns.can.energy.service.DrivingEnergyService;
import com.hns.can.energy.vo.NewEnergy;
import com.hns.can.master.entity.CommEnerConsStatInfoEntity;
import com.hns.frame.enumrate.MsgType;
import com.hns.frame.exception.BusinessException;
import com.hns.frame.mybatis.Pagination;
import com.hns.frame.util.ResponseUtil;
import com.hns.tool.export.ExportUtil;
import com.hns.tool.export.excel.excelNoTemplate.ExportExcelNoTempEntity;
import com.hns.tool.export.pdf.ExportPdfEntity;
import com.hns.tool.pub.DateTool;
import com.hns.tool.pub.PubUtil;

/**
 * 驾驶员能耗
 * @author:Fisher
 * @email:zhuangcaijin@126.com
 * @version Revision 2.0.0
 */
@Controller
@RequestMapping("/drivingEnergy")
public class DrivingEnergyController extends BaseController{
    
    @Autowired
    DrivingEnergyService drivingEnergyService;
    @Autowired
    private CommonInitParamService commonInitParamService;
    
    final String[] headerNames = {"驾驶员", "车牌号", "车辆编号", "营运线路", "所属机构", "车辆类型", 
        "电机型号", "运营日期", "运行时长", "运行里程", "燃料类型", "运行能耗", "百公里能耗", "标准能耗", "能耗百分比", "线路平均能耗", "是否节能" };
    
    final String[] headerFields={"drvName","licPltNo","carInCd","lineName","organName",
        "carType","engiNo","days","rngOprtDuraTr","rngMile","fuelType","rngEnerCons",
        "enerConsMile","refrEnerCons","enerConsPers","lineEnerConsMile","isSaveEner"};
    
    /**
     * 查询
     * @param vo
     * @param response
     * @param request
     * @throws Exception
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     */
    @RequestMapping("/searchDrivingEnergy")
    @ResponseBody
    public Object searchDrivingEnergy(NewEnergy vo, HttpServletResponse response, HttpServletRequest request) throws Exception {
    	if(PubUtil.isNotEmpty(vo.getDateType()) && PubUtil.isNotEmpty(vo.getEnergyType())){
    		commonInitParamService.dateType2DateTime(vo, vo.getDateType());
    	}
    	
        if (vo.getOrgIds() == null || vo.getBeginTime() == null || vo.getEndTime() == null) {
            ResponseUtil.responseJSON(null, response);
            return this.getReturnResult();
        }
        SysUserEntity user = this.getCurrentUser().getUser();
        vo.setCorpId(user.getCorpId());
        vo.setDriverRelTableName("T_COMM_ENER_CONS_STAT_INFO");
        
        Pagination page = drivingEnergyService.searchDrivingEnergy(vo);
        GridPage grid = new GridPage(page);
        return this.getReturnResult(grid);
//        ResponseUtil.responseJSON(grid, response);
    }
    
    /**
     * 导出excel
     * @param vo
     * @param request
     * @param response
     * @throws Exception
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     */
    @RequestMapping("/exportDrivingEnergyExcel")
    public void exportDrivingEnergyExcel(NewEnergy vo, HttpServletRequest request, HttpServletResponse response) throws Exception {
        SysUserEntity user = this.getCurrentUser().getUser();
        vo.setCorpId(user.getCorpId());
        List<CommEnerConsStatInfoEntity> list = drivingEnergyService.exportDrivingEnergy(vo);
        if (PubUtil.isEmpty(list)) {
            throw new BusinessException(MsgType.WARN.getFlag(), "没有数据导出！");
        }
        
        ExportExcelNoTempEntity exportEntity = new ExportExcelNoTempEntity(
            Arrays.asList(headerNames), Arrays.asList(headerFields), list, URLEncoder.encode("驾驶员能耗明细", "UTF-8")+DateTool.getCurrentSysDate("yyyyMMddHHmmss"));
        ExportUtil.generatorExcelNoTemplate(exportEntity, response);
    }
    
    /**
     * 导出Pdf
     * @param vo
     * @param request
     * @param response
     * @throws Exception
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     */
    @RequestMapping("/exportDrivingEnergyPdf")
    public void exportDrivingEnergyPdf(NewEnergy vo, HttpServletRequest request, HttpServletResponse response) throws Exception {
        SysUserEntity user = this.getCurrentUser().getUser();
        vo.setCorpId(user.getCorpId());
        List<CommEnerConsStatInfoEntity> list = drivingEnergyService.exportDrivingEnergy(vo);
        if (PubUtil.isEmpty(list)) {
            throw new BusinessException(MsgType.WARN.getFlag(), "没有数据导出！");
        }
        
        ExportPdfEntity pdfEntity = new ExportPdfEntity(
            Arrays.asList(headerNames), Arrays.asList(headerFields), list, URLEncoder.encode("驾驶员能耗明细", "UTF-8")+DateTool.getCurrentSysDate("yyyyMMddHHmmss"));
        
        ExportUtil.generatorPDF(pdfEntity, response);
    }
    
    /**
     * 查询
     * @param vo
     * @param response
     * @param request
     * @throws Exception
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     */
    @RequestMapping("/getEnergyDetail")
    @ResponseBody
    public Object searchEnergyDetail(NewEnergy vo, HttpServletResponse response, HttpServletRequest request) throws Exception {
    	commonInitParamService.dateType2DateTime(vo, vo.getDateType());
        SysUserEntity user = this.getCurrentUser().getUser();
        vo.setCorpId(user.getCorpId());
        vo.setDriverRelTableName("T_COMM_ENER_CONS_STAT_INFO");
        Pagination page = drivingEnergyService.searchDrivingEnergy(vo);
        GridPage grid = new GridPage(page);
        return this.getReturnResult(grid);
//        ResponseUtil.responseJSON(grid, response);
    }
}
